<?php

include_once ('bn_path.php');

class bn_directory
{
    protected static $instance;
    
    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    public function get_dirlist($path, $type = 'all', $extention = '')
    {
        $list = array();
        $path = bn_add_endpath_slash($path);
        if (is_dir($path))
        {
            if ($dh = opendir($path))
            {
                while ($file = readdir($dh))
                {
                    if ($file == '.' || $file == '..')
                        continue;
                    else
                    {
                        $shpath = $path . $file;
                        switch ($type)
                        {

                            case "all":
                                if (empty($extention))
                                    $list[] = $path . $file;
                                else
                                {
                                    if (is_dir($shpath))
                                        $list[] = $path . $file;
                                    else
                                    {
                                        if (bn_check_fileEXT($path . $file, $extention))
                                            $list[] = $path . $file;
                                    }
                                }
                                break;


                            case "dir":
                                if (is_dir($shpath))
                                    $list[] = $path . $file;
                                break;


                            case "file":
                                if (is_file($shpath))
                                {
                                    if (empty($extention))
                                        $list[] = $path . $file;
                                    else
                                    {
                                        if (bn_check_fileEXT($path . $file, $extention))
                                            $list[] = $path . $file;
                                    }
                                }
                                break;
                            default:
                                return 0;
                                break;
                        }
                    }
                }
            }
            closedir($dh);
        }
        return $list;
    }


    public function get_dirlist_info($path, $type = 'all', $extention = '')
    {
        $list = array();
        $path = bn_add_endpath_slash($path);
        if (is_dir($path))
        {
            if ($dh = opendir($path))
            {
                while ($file = readdir($dh))
                {
                    if ($file == '.' || $file == '..')
                        continue;
                    else
                    {
                        $shpath = $path . $file;
                        switch ($type)
                        {

                            case "all":
                                if (empty($extention))
                                {
                                    echo "empty ext";
                                    $list[] = pathinfo($path . $file);
                                } else
                                {
                                    if (is_dir($shpath))
                                        $list[] = pathinfo($path . $file);
                                    else
                                    {
                                        if (bn_check_fileEXT($path . $file, $extention))
                                            $list[] = pathinfo($path . $file);
                                    }
                                }
                                break;


                            case "dir":
                                if (is_dir($shpath))
                                    $list[] = pathinfo($path . $file);
                                break;


                            case "file":
                                if (is_file($shpath))
                                {
                                    if (empty($extention))
                                        $list[] = pathinfo($path . $file);
                                    else
                                    {
                                        if (bn_check_fileEXT($path . $file, $extention))
                                            $list[] = pathinfo($path . $file);
                                    }
                                }
                                break;
                            default:
                                return 0;
                                break;
                        }
                    }
                }
            }
            closedir($dh);
        }
        return $list;
    }


    public function get_allsubdir($path, $EXT = '')
    {
        $list = array();
        $path = bn_add_endpath_slash($path);
        if (is_dir($path))
        {
            $list = self::get_dirlist($path, 'all', $EXT);
            foreach (self::get_dirlist($path, 'dir') as $dir)
            {
                $list = array_merge($list, self::get_allsubdir($dir));
            }
        }
        return $list;
    }

    public function get_allsubdir_callback($path, $callback, $count = -1)
    {
        $list = array();
        if ($count == 0)
            return $list;
        --$count;

        $path = bn_add_endpath_slash($path);
        if (is_dir($path))
        {
            foreach (self::get_dirlist($path, 'all') as $dir)
            {
                $mdir = $dir;
                if (is_callable($callback))
                    $mdir = call_user_func($callback, $dir);
                if (!empty($mdir))
                    $list[] = $mdir;
                if (is_dir($dir))
                    $list = array_merge($list, self::get_allsubdir_callback($dir, $callback, $count));
            }
        }
        return $list;
    }

    public function Delete_dir($path, $self = true)
    {
        $list = self::get_allsubdir($path);
        arsort($list);
        foreach ($list as $file)
        {
            if (is_dir($file))
                rmdir($file);
            else
                unlink($file);
        }
        if ($self)
            rmdir($path);
    }

    public function is_dir_empty($path)
    {
        $list = self::get_dirlist($path, 'all');
        if (empty($list))
            return true;
        else
            return false;
    }

}

?>